/**
 * Created by tony on 16/3/18.
 */
var xmlHttp;
function createXMLHttpRequest() {
    if (window.ActiveXObject) {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else if (window.XMLHttpRequest) {
        xmlHttp = new XMLHttpRequest();
    }
    return xmlHttp;
}

function setSelectChecked(selectId, checkValue) {
    var select = document.getElementById(selectId);
    for (var i = 0; i < select.options.length; i++) {
        if (select.options[i].value == checkValue) {
            select.options[i].selected = true;
            break;
        }
    }
}

function getRadioBoxValue(radioName) {
    var obj = document.getElementsByName(radioName);
    for (i = 0; i < obj.length; i++) {
        if (obj[i].checked) {
            return obj[i].value;
        }
    }
    return "3";
}

function check(obj) {
    for (i = 0; i < document.getElementsByName(obj).length; i++) {
        if (document.getElementsByName(obj)[i].checked) {
            return true;
        }
    }
    return false;
}

//通过专业获取班,班 ID=class
function GetClassItems(procode) {
    createXMLHttpRequest();
    var queryString = '/Listitems/getClassArray/procode/' + procode;
    xmlHttp.onreadystatechange = handleStateChangeClassItems;
    xmlHttp.open("GET", queryString, true);
    xmlHttp.send(null);
}
function handleStateChangeClassItems() {
    if (xmlHttp.readyState == 4) {
        if (xmlHttp.status == 200) {
            if (xmlHttp.responseText.length > 0) {
                if (xmlHttp.responseText == "error") {
                    alert("服务器通讯失败!");
                }
                else {
                    var json = eval("(" + xmlHttp.responseText + ")");
                    classlist = json.data;
                    var classcode = "";
                    var classname = "";
                    document.getElementById("class").length = 0;
                    var optclass = document.createElement("Option");
                    if (classlist.length == 0) {
                        optclass.text = "---没有可选班级---";
                    } else {
                        optclass.text = "请选择班级";
                    }
                    optclass.value = "0";
                    document.getElementById("class").options.add(optclass);

                    for (i = 0; i <= classlist.length - 1; i++) {
                        classcode = classlist[i].ClassCode.toString();
                        classname = classlist[i].ClassName.toString();
                        var opt = document.createElement("Option");
                        opt.text = classname;
                        opt.value = classcode;
                        document.getElementById("class").options.add(opt);
                    }
                }
            }
        }
    }
}

//通过院系编号获取专业,专业 ID=profe
function GetProfeItems(depcode) {
    createXMLHttpRequest();
    var queryString = '/Listitems/getProArray/depcode/' + depcode;
    xmlHttp.onreadystatechange = handleStateChangeProfeItems;
    xmlHttp.open("GET", queryString, true);
    xmlHttp.send(null);
}
function handleStateChangeProfeItems() {
    if (xmlHttp.readyState == 4) {
        if (xmlHttp.status == 200) {
            if (xmlHttp.responseText.length > 0) {
                if (xmlHttp.responseText == "error") {
                    alert("error!");
                }
                else {
                    var json = eval("(" + xmlHttp.responseText + ")");
                    prolist = json.data;
                    var procode = "";
                    var proname = "";
                    document.getElementById("profe").length = 0;
                    obj = document.getElementById("class");
                    if (obj) {
                        obj.length=0;
                        var optclass = document.createElement("Option");
                        optclass.text = "";
                        optclass.value = "0";
                        obj.options.add(optclass);
                    }
                    var opt = document.createElement("Option");
                    if (prolist.length > 0) {
                        opt.text = "请选择专业";
                    } else {
                        opt.text = "---没有可选专业---";
                    }
                    opt.value = "0";

                    document.getElementById("profe").options.add(opt);
                    for (i = 0; i <= prolist.length - 1; i++) {
                        procode = prolist[i].ProCode.toString();
                        proname = prolist[i].ProName.toString();
                        var opt = document.createElement("Option");
                        opt.text = proname;
                        opt.value = procode;
                        document.getElementById("profe").options.add(opt);
                    }
                }
            }
        }
    }
}

$("#full-screen").click(function () {
    var elem = document.body;
    var me=true;
    if (elem.webkitRequestFullScreen) {
        elem.webkitRequestFullScreen();
    } else if (elem.mozRequestFullScreen) {
        elem.mozRequestFullScreen();
    } else if (elem.requestFullScreen) {
        elem.requestFullscreen();
    }else if (elem.msRequestFullscreen) {
        elem.msRequestFullscreen();
    }else{
        me=false;
        $.messager.popup('对不起,该浏览器不支持全屏API或已被禁用');
        //浏览器不支持全屏API或已被禁用
    }
    if(me){
        $(this).hide();
        $("#exit-screen").show();
    }
});
$("#exit-screen").click(function () {
    var elem = document;
    var me=true;
    if (elem.webkitCancelFullScreen) {
        elem.webkitCancelFullScreen();
    } else if (elem.mozCancelFullScreen) {
        elem.mozCancelFullScreen();
    } else if (elem.exitFullscreen) {
        elem.exitFullscreen();
    }else if (elem.msExitFullscreen) {
        elem.msExitFullscreen();
    }else{
        me=false;
        $.messager.popup('对不起,该浏览器不支持全屏API或已被禁用');
        //浏览器不支持全屏API或已被禁用
    }
    if(me){
        $(this).hide();
        $("#full-screen").show();
    }
});

//去除两端空格
String.prototype.trim=function()
{
    return this.replace(/(^\s*)|(\s*$)/g, '');
}


function stripscript(s) {
    var pattern = new RegExp("[`~!#$^&*()=-_|{}':;',\\[\\].<>/?~！@#￥……&*（）&;|{}【】‘；：”“'。，、？]");
    var rs = "";
    for (var i = 0; i < s.length; i++) {
        rs = rs + s.substr(i, 1).replace(pattern, '');
    }
    return rs;
}

function html_encode(str)   
{   
  var s = "";   
  if (str.length == 0) return "";   
  s = str.replace(/&/g, "&gt;");   
  s = s.replace(/</g, "&lt;");   
  s = s.replace(/>/g, "&gt;");   
  s = s.replace(/ /g, "&nbsp;");   
  s = s.replace(/\'/g, "&#39;");   
  s = s.replace(/\"/g, "&quot;");   
  s = s.replace(/\n/g, "<br>");   
  return s;   
}   
 
function html_decode(str)   
{   
  var s = "";   
  if (str.length == 0) return "";   
  s = str.replace(/&gt;/g, "&");   
  s = s.replace(/&lt;/g, "<");   
  s = s.replace(/&gt;/g, ">");   
  s = s.replace(/&nbsp;/g, " ");   
  s = s.replace(/&#39;/g, "\'");   
  s = s.replace(/&quot;/g, "\"");   
  s = s.replace(/<br>/g, "\n");   
  return s;   
}
